<template>
  <el-button ref="elButton">渲染按钮</el-button>
</template>
<script>
export default {
  name: "renderButton",
  props: {
    block: {
      type: Object,
    },
  },
  data() {
    return {};
  },
  created() {
    const myFn = new Function("a", "b", "console.log(a+b)");
    myFn("hello ", "lzc");
  },
  mounted() {
    if (this.block?.event && Object.keys(this.block.event).length) {
      for (const eventType in this.block.event) {
        this.$refs.elButton.$el.addEventListener(eventType, () => {
          let argument = [];
          if (this.block.event[eventType].argument?.length) {
            argument = this.block.event[eventType].argument;
          }

          const fn = Function(
            ...argument,
            this.block.event[eventType].statement
          );

          fn.call(this);
        });
      }
    }
  },
  methods: {},
  destroyed() {
    if (this.block?.event && Object.keys(this.block.event).length) {
      for (const eventType in this.block.event) {
        this.$refs.elButton.$el.addEventListener(eventType, () => {
          let argument = [];
          if (this.block.event[eventType].argument?.length) {
            argument = this.block.event[eventType].argument;
          }

          const fn = Function(
            ...argument,
            this.block.event[eventType].statement
          );

          fn.call(this);
        });
      }
    }
  },
};
</script>
<style lang="scss" scoped></style>
